<?php

namespace app\api\controller\shop;

use app\admin\model\Cate;
use app\admin\model\Goods;
use app\common\controller\Api;

/**
 * 首页接口
 */
class Good extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];



    public function addCate()
    {
        $user = $this->auth->getUser();
        $name = $this->request->param('name');

        $cateModel = new Cate();
        $cate = $cateModel->where('name',$name)->where('user_id',$user['id'])->find();
        if($cate){
            $this->error("此分类已存在");
        }
        $cateModel->insert(['user_id'=>$user['id'],'name'=>$name]);

        $this->success('成功');

    }

    public function cateList()
    {
        $user = $this->auth->getUser();
        $cateModel = new Cate();
        $goodsModel = new Goods();
        $list = $cateModel->where('user_id',$user['id'])->select();
        foreach ($list as $key=>$val){
            $goodCount = $goodsModel->where('cate_id',$val['id'])->count('id');
            $list[$key]['goodCount'] = $goodCount;
        }

        $this->success('',$list);
    }

    public function editCate()
    {

        $name = $this->request->param('name');
        $id = $this->request->param('id');

        $cateModel = new Cate();
        $cateModel->where('id',$id)->update(['name'=>$name]);

        $this->success('成功');
    }


    public function cateDel()
    {

        $user = $this->auth->getUser();
        $id = $this->request->param('id');
        $cateModel = new Cate();
        $cateModel->where('id',$id)->where('user_id',$user['id'])->delete();
        $this->success('成功');

    }

    public function goodList()
    {
        $id = $this->request->param('id');
        $status = $this->request->param('status');
        $keyword = $this->request->param('keyword');

        $user = $this->auth->getUser();
        $goodModel = new Goods();
        $where = array();
        $where['user_id'] = $user['id'];
        if($id != ""){
            $where['cate_id'] = $id;
        }
        if($status != -1){
            $where['status'] = $status;

        }
        if($keyword != ""){
            $where['name'] = ['like', '%' . trim($keyword) . '%'];

        }

        $list = $goodModel->where($where)->select();
        foreach ($list as $key=>$val){
            $list[$key]['cover'] = cdnurl($val['cover'],true);
        }
        $this->success('',$list);
    }

    public function addGood()
    {

        $user = $this->auth->getUser();
        $cateId = $this->request->param('cateId');
        $name = $this->request->param('name');
        $price = $this->request->param('price');
        $status = $this->request->param('status');
        $coin = $this->request->param('coin');
        $image = $this->request->param('image');

        if(!$coin){
            $coin = 0;
        }

        $db = array();
        $db['name'] = $name;
        $db['cate_id'] = $cateId;
        $db['user_id'] = $user['id'];
        $db['cover'] = $image;
        $db['price'] = $price;
        $db['coin'] = $coin;
        $db['status'] = $status;


        $goodModel = new Goods();
        $goodModel->insert($db);
        $this->success('成功');

    }

    public function getGood()
    {
        $id = $this->request->param('id');
        $goodModel =new Goods();
        $good = $goodModel->where('id',$id)->find();
        $cateModel = new Cate();

        if(!$good) $this->error("商品不存在");
        $cate = $cateModel->where('id',$good['cate_id'])->find();
        $good['image'] = cdnurl($good['cover'],true);
        $good['cateName'] =$cate['name'];
        $this->success('',$good);
    }

    public function editGood()
    {

        $cateId = $this->request->param('cateId');
        $name = $this->request->param('name');
        $price = $this->request->param('price');
        $status = $this->request->param('status');
        $coin = $this->request->param('coin');
        $image = $this->request->param('image');
        $id = $this->request->param('id');

        if(!$coin){
            $coin = 0;
        }



        $db = array();
        $db['name'] = $name;
        $db['cate_id'] = $cateId;
        $db['cover'] = $image;
        $db['price'] = $price;
        $db['coin'] = $coin;
        $db['status'] = $status;


        $goodModel = new Goods();
        $goodModel->where('id',$id)->update($db);
        $this->success('成功');
    }
}
